home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 4838 / 4838.xpi / chrome / multipletab.jar / content / multipletab / lib / prefs.js < prev    next >
Encoding:
JavaScript  |  2010-02-03  |  3.6 KB  |  129 lines

  1. /*
  2.  Preferences Library
  3.  
  4.  Usage:
  5.    var value = window['piro.sakura.ne.jp'].prefs.getPref('my.extension.pref');
  6.    window['piro.sakura.ne.jp'].prefs.setPref('my.extension.pref', true);
  7.    window['piro.sakura.ne.jp'].prefs.clearPref('my.extension.pref');
  8.    var listener = {
  9.          domains : [
  10.            'browser.tabs',
  11.            'extensions.someextension'
  12.          ],
  13.          observe : function(aSubject, aTopic, aData)
  14.          {
  15.            if (aTopic != 'nsPref:changed') return;
  16.            var value = window['piro.sakura.ne.jp'].prefs.getPref(aData);
  17.          }
  18.        };
  19.    window['piro.sakura.ne.jp'].prefs.addPrefListener(listener);
  20.    window['piro.sakura.ne.jp'].prefs.removePrefListener(listener);
  21.  
  22.  lisence: The MIT License, Copyright (c) 2009-2010 SHIMODA "Piro" Hiroshi
  23.    http://www.cozmixng.org/repos/piro/fx3-compatibility-lib/trunk/license.txt
  24.  original:
  25.    http://www.cozmixng.org/repos/piro/fx3-compatibility-lib/trunk/prefs.js
  26.    http://www.cozmixng.org/repos/piro/fx3-compatibility-lib/trunk/prefs.test.js
  27. */
  28. (function() {
  29.     const currentRevision = 4;
  30.  
  31.     if (!('piro.sakura.ne.jp' in window)) window['piro.sakura.ne.jp'] = {};
  32.  
  33.     var loadedRevision = 'prefs' in window['piro.sakura.ne.jp'] ?
  34.             window['piro.sakura.ne.jp'].prefs.revision :
  35.             0 ;
  36.     if (loadedRevision && loadedRevision > currentRevision) {
  37.         return;
  38.     }
  39.  
  40.     const Cc = Components.classes;
  41.     const Ci = Components.interfaces;
  42.  
  43.     window['piro.sakura.ne.jp'].prefs = {
  44.         revision : currentRevision,
  45.  
  46.         Prefs : Cc['@mozilla.org/preferences;1']
  47.                     .getService(Ci.nsIPrefBranch)
  48.                     .QueryInterface(Ci.nsIPrefBranch2),
  49.  
  50.         DefaultPrefs : Cc['@mozilla.org/preferences-service;1']
  51.                     .getService(Ci.nsIPrefService)
  52.                     .getDefaultBranch(null),
  53.      
  54.         getPref : function(aPrefstring, aBranch) 
  55.         {
  56.             if (!aBranch) aBranch = this.Prefs;
  57.             switch (aBranch.getPrefType(aPrefstring))
  58.             {
  59.                 case aBranch.PREF_STRING:
  60.                     return decodeURIComponent(escape(aBranch.getCharPref(aPrefstring)));
  61.  
  62.                 case aBranch.PREF_INT:
  63.                     return aBranch.getIntPref(aPrefstring);
  64.  
  65.                 case aBranch.PREF_BOOL:
  66.                     return aBranch.getBoolPref(aPrefstring);
  67.  
  68.                 case aBranch.PREF_INVALID:
  69.                 default:
  70.                     return null;
  71.             }
  72.         },
  73.  
  74.         getDefaultPref : function(aPrefstring)
  75.         {
  76.             return this.getPref(aPrefstring, this.DefaultPrefs);
  77.         },
  78.      
  79.         setPref : function(aPrefstring, aNewValue, aBranch) 
  80.         {
  81.             if (!aBranch) aBranch = this.Prefs;
  82.             switch (typeof aNewValue)
  83.             {
  84.                 case 'string':
  85.                     return aBranch.setCharPref(aPrefstring, unescape(encodeURIComponent(aNewValue)));
  86.  
  87.                 case 'number':
  88.                     return aBranch.setIntPref(aPrefstring, parseInt(aNewValue));
  89.  
  90.                 default:
  91.                     return aBranch.setBoolPref(aPrefstring, aNewValue);
  92.             }
  93.         },
  94.  
  95.         setDefaultPref : function(aPrefstring, aNewValue)
  96.         {
  97.             return this.setPref(aPrefstring, aNewValue, this.DefaultPrefs);
  98.         },
  99.      
  100.         clearPref : function(aPrefstring) 
  101.         {
  102.             if (this.Prefs.prefHasUserValue(aPrefstring))
  103.                 this.Prefs.clearUserPref(aPrefstring);
  104.         },
  105.      
  106.         addPrefListener : function(aObserver) 
  107.         {
  108.             var domains = ('domains' in aObserver) ? aObserver.domains : [aObserver.domain] ;
  109.             try {
  110.                 for each (var domain in domains)
  111.                     this.Prefs.addObserver(domain, aObserver, false);
  112.             }
  113.             catch(e) {
  114.             }
  115.         },
  116.      
  117.         removePrefListener : function(aObserver) 
  118.         {
  119.             var domains = ('domains' in aObserver) ? aObserver.domains : [aObserver.domain] ;
  120.             try {
  121.                 for each (var domain in domains)
  122.                     this.Prefs.removeObserver(domain, aObserver, false);
  123.             }
  124.             catch(e) {
  125.             }
  126.         }
  127.     };
  128. })();
  129.